#include "myitem.h"
#include <QPixmap>
#include <QDebug>
//p28
#include "handler.h"
#include <QCursor>
myItem::myItem()
{
    //QGraphicsItem里的qgraphicspixmapitem有一个setPixmap函数参数里面是一个类函数
    //QPixmap(const QString &fileName, const char *format = nullptr,
    // Qt::ImageConversionFlags flags = Qt::AutoColor)
    //得知QPixmap的第一个参数需要一个文件名。后面参数是默认传参
    //一、这样就把图片封装到图元里面去了
    this->setPixmap(QPixmap(":/bg/pic/bg.png"));
    this->start = false;
    this->mouse = false;
    this->setCursor(QCursor(QPixmap(":/mous/pic/chui_up")));
}

//p25:一、换图元的函数，被使用在myscene.cpp中
void myItem::setPic(QString path){
    this->setPixmap(QPixmap(path));
}

void myItem::mousePressEvent(QGraphicsSceneMouseEvent *event){
    this->setCursor(QCursor(QPixmap(":/mous/pic/chui_down")));

    if(this->isStart()){
        //p28
        handler * hand = handler::getInstance();
        //p26:每点击一次都会判断this->mouse的bool值。来处理相应的事件
        if(this->isMouse()){
            //p28：这里会发送一个自定义的信号emit beatMouse();
            //当hand中addScore函数被调用的时候会发出一个beatMouse()信号
            hand->addScore();
            this->setPixmap(QPixmap(":/mous/pic/beat.png"));
            this->mouse = false;
        }
    }
}
void myItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    this->setCursor(QCursor(QPixmap(":/mous/pic/chui_up")));
}

void myItem::setMouse(bool mouse){
    this->mouse = mouse;

}
bool myItem::isMouse(){
    return this->mouse;
}

void myItem::setStart(bool start){
    this->start = start;
}
bool myItem::isStart(){
    return this->start;
}
